package com.inspectortime.webapp.payment;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.inspectortime.domain.Inspection;
import com.inspectortime.domain.Inspector;
import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.domain.billing.ConfirmedStatus;
import com.inspectortime.domain.billing.Invoice;
import com.inspectortime.domain.billing.Payment;
import com.inspectortime.domain.billing.PaymentLineItem;
import com.inspectortime.domain.billing.PaymentMethod;
import com.inspectortime.domain.billing.PaymentSourceType;
import com.inspectortime.domain.type.PaymentStatus;
import com.inspectortime.repository.Repository;
import com.mkode.util.servlet.RequestDebugger;

/**
 * Controller for a form POST callback from Authorize.net. Note this URL is
 * configured in the Authorize.Net merchant interface.
 * 
 * @author mkvalsvik
 * 
 */
public abstract class AbstractConfirmationCallbackResponseController implements
		Controller {

	private static Log log = LogFactory
			.getLog(AbstractConfirmationCallbackResponseController.class);
	protected Repository repository;

	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	protected ModelAndView showError(String message) {
		ModelAndView mv = new ModelAndView("error");
		mv.addObject("message", message);
		return mv;
	}

	protected ModelAndView showDefaultError() {
		return showError("An error has occurred.  We are unable to provide you with a receipt at this time.  Please check your e-mail messages for an electronic receipt. <br/><br/>-The support staff at Inspector Time (<a href='http://www.inspectortime.com/it/support.html'>contact us</a>)");
	}

	/**
	 * Use first line item to find source and company
	 * 
	 * @param payment
	 * @return
	 */
	protected InspectorCompany lookupCompany(Payment payment) {

		PaymentSourceType type = payment.getPaymentSourceType();
		PaymentLineItem firstLineItem = payment.getLineItems().iterator()
				.next();
		String sourceId = firstLineItem.getPaymentSourceId();
		Object paymentSource = repository.findPaymentSource(sourceId, type);
		String companyId = null;

		// Different approach based on type of source
		if (paymentSource instanceof Invoice) {
			companyId = ((Invoice) paymentSource).getCompanyId();
		} else if (paymentSource instanceof Inspection) {
			Inspection inspection = (Inspection) paymentSource;
			Inspector inspector = inspection.getInspector();
			InspectorCompany company = inspector.getCompany();
			companyId = (String) company.getId();
		}
		InspectorCompany company = repository.loadInspectorCompany(companyId);
		return company;
	}
	
	protected void updatePaymentSourceStatus(Payment payment, PaymentStatus inspectionPaymentStatus) {
		PaymentSourceType type = payment.getPaymentSourceType();
		PaymentLineItem firstLineItem = payment.getLineItems().iterator()
		.next();
		String sourceId = firstLineItem.getPaymentSourceId();
		Object paymentSource = repository.findPaymentSource(sourceId, type);
		if (paymentSource instanceof Inspection) {
			Inspection inspection = (Inspection) paymentSource;
			inspection.setPaymentStatus(inspectionPaymentStatus);
			inspection.save();
		}		
	}

}
